import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * This class is the mainController where all the controls for the classes are
 * @author paulsmith
 *
 */
public class MainController 
{
        private InventoryModel im;
        private MainView mv;
        private StatusModel sm;
        private StatusView sv;
        private StatusController sc;
        private MapController mpc;
        private MapView map;
        private WinView win;
        private MilestoneModel mm;
        private MilestoneView msv;
        private StoreView stv;
        private MainPlayerModel mpm;
        private RandomEventGenerator rg;
        
        /**
         * This is the constructor where the game's models, views and controllers
         * @param im    im is InventoryModel
         * @param mv    mv is MainView
         * @param sm    sm is StatusModel
         * @param sv    sv is StatusView
         * @param sc    sc is StatusController
         * @param map   map is mapView
         * @param mpc   mpc is mapViewController
         * @param win   win is WinView
         * @param mm    mm is MilestoneModel
         * @param msv   msv is MileStoneView
         */
        public MainController(InventoryModel im, MainView mv, StatusModel sm, StatusView sv, StatusController sc, MapView map,
        						MapController mpc, WinView win, MilestoneModel mm, MilestoneView msv, StoreView stv, MainPlayerModel mpm,
        						RandomEventGenerator rg)
        {
                this.im = im;
                this.mv = mv;
                this.sm = sm;
                this.sv = sv;
                this.sc = sc;
                this.map = map;
                this.mpc = mpc;
                this.win = win;
                this.mm = mm;
                this.msv = msv;
                this.stv = stv;
                this.mpm = mpm;
                this.rg = rg;
                
                mv.addBtnListener(new ButtonListener());
        }
        
        /**
         * This is the buttonlistener for the button in the mapView
         * @author paulsmith
         *
         */
        
        private class ButtonListener implements ActionListener 
        {
                public void actionPerformed(ActionEvent e) 
                {
                        if(e.getSource().equals(mv.getRationsAndPaceBtn()))
                        {
                                sv.setBounds(500, 200, 350, 300);
                                sv.setOkBtnLbl("Continue Roadtrip");
                                //sc.setWhetherUserIsDone(false);
                                sv.setVisible(true);
                
                
                                
                        }
                        else if(e.getSource().equals(mv.getTakeTurnBtn()))
                        {
                        	 stv.setVisible(false);
                             
                             mpc.movePlayer(sm.getPaceIndex()*30);
                             /*while(mpc.getPlayerX()>mm.getSpotX(mpc.getNextIndex())&&mpc.getPlayerX()<(mm.getSpotX(mpc.getNextIndex())+70)){
                             msv.setVisible(true);
                     }*/

                             mv.repaint();
                             sm.updateDist();
                             
                             for(int i = 0; i < sm.getRationIndex(); i++)
                             {
                                     im.getItems().get(1).decrementQuantity();
                             }
                             
                             mv.setFoodLbs(im.getItems().get(1).getQuantity());
                             mv.setDistArea(sm.getDistTravelled());
                             
                             if(mpc.getNextIndex() == 15)
                             {
                             	mv.setVisible(false);
                             	win.setVisible(true);
                             }
                             
                             JOptionPane.showMessageDialog(new JFrame("Event!"), rg.doRandom());
                        }
                        else
                        {
                        	 try
                        	 {
                        		 String passCode = "";
                        		 char symbol;
                        		 
                        		 for(int i = 0; i < 8; i++)
                        		 {
                        			 symbol = (char)(Math.random() * 94 + 33); //
                        			 passCode += symbol;
                        		 }
                        		 
                        		 JOptionPane.showMessageDialog(new JFrame("Congratulations!"),
                                         "You have successfully saved your game! Remember your password! " +
                                         "Password: " + passCode);
                        		 System.out.println("NumPartyMembers when loading from MainController: " + mpm.getNumPartyMembers());
                        		 
                        		 FileWriter out = new FileWriter("saveFile.txt", true);
                        		 out.write("" + passCode + " " + sm.getDistTravelled() + " " + sm.getPaceIndex() + " " + sm.getRationIndex() 
                        				   + " " + im.getQuantitiesStr() + mpm.getProfessionIndex() + " " + mpm.getNumPartyMembers() + " "  
                        				   + mpm.getPartyMembersInOneStr() + mpm.getTotalCash() + " " + mpm.getName()
                        				   + " " + mpc.getPlayerX() + " " + mpc.getPlayerY() + " " + mpc.getNextIndex() + "\n");	
                        		
                        		 out.close();
                        		 
							 }
                        	  catch (IOException a) {
								System.err.println("Failed saving file");
								a.printStackTrace();
							}
                        }
                }
        }
}
